package com.groupseven.happycampus.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.groupseven.happycampus.common.bo.BicycleBo;
import com.groupseven.happycampus.common.vo.BicycleBrandPieVo;
import com.groupseven.happycampus.common.vo.BicycleColorBarVo;
import com.groupseven.happycampus.common.vo.LayuiPageVo;
import com.groupseven.happycampus.mapper.BicycleMapper;
import com.groupseven.happycampus.common.pojo.Bicycle;
import com.groupseven.happycampus.service.BicycleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("bicycleService")
public class BicycleServiceImpl implements BicycleService {

    @Autowired
    private BicycleMapper bicycleMapper;

    @Override
    public LayuiPageVo<Bicycle> bicycleListPageData(Map<String, Object> params) {
        Integer page = Integer.parseInt(params.get("page") + "");
        Integer limit = Integer.parseInt(params.get("limit") + "");

        PageHelper.startPage(page, limit);
        //从数据库中查到的数据放进 List 集合
        List<Bicycle> bicycles = bicycleMapper.bicycleListSelect(params);

//        System.out.println("====="+bicycles);

        //将获取到的 bicycles 集合赋值给 PageInfo 类型的 bicyclePageInfo 对象，为下面的分页操作做准备
        PageInfo<Bicycle> bicyclePageInfo = new PageInfo<>(bicycles);
        //创建一个 LayuiPageVo 类型的 bicycleLayuiPageVo 对象，用来存放后面的 R 类型的数据
        LayuiPageVo<Bicycle> bicycleLayuiPageVo = new LayuiPageVo<>();
        bicycleLayuiPageVo.setCode(0);// 赋值为 0 在 Layui 表示成功的状态码
        bicycleLayuiPageVo.setMsg("分页数据");// 为 Msg 赋值
        // 从 bicyclePageInfo 对象中获取数据的中总页数
        bicycleLayuiPageVo.setCount(bicyclePageInfo.getTotal());
        // 从 bicyclePageInfo 对象中获取数据的中总条数
        bicycleLayuiPageVo.setData(bicyclePageInfo.getList());
        return bicycleLayuiPageVo;
    }

    @Override
    public int bicycleAdd(Bicycle bicycle) {
        return bicycleMapper.bicycleAdd(bicycle);
    }

    @Override
    public int bicycleUpdate(Bicycle bicycle) {
        int result = bicycleMapper.bicycleUpdate(bicycle);
        return result;
    }

    @Override
    public int bicycleDelete(Integer id) {
        int result = bicycleMapper.bicycleDelete(id);
        System.out.println("result:" + result);
        return result;
    }

    @Override
    public int deleteBicycleByIds(String ids) {
        String [] arrId = ids.split(",");
        return bicycleMapper.deleteBicycleByIds(arrId);
    }

    @Override
    public List<BicycleColorBarVo> colorBarData() {
        List<BicycleColorBarVo> list = bicycleMapper.colorBarData();
        System.out.println(list);
        return list;
    }

    @Override
    public List<BicycleBrandPieVo> brandPieData() {
        return bicycleMapper.brandPieData();
    }

    @Override
    public List<BicycleBo> bicycleExcelData(Bicycle bicycle) {
        //首先通过 前端传来的一个bicycle对象，这个对象若没有模糊查询就是整个表的所有数据，如果有模糊查询，则是模糊查询后的数据
        //查询后的数据是一个list集合，里面存放有多个对象，通过流的方式将Bicycle对象转换为我们想要的BicycleBo对象，
        //最后获取Bicycle对象的属性将其赋值给BicycleBo对象
        List<Bicycle> bicycles = bicycleMapper.bicycleExcelData(bicycle);

        List<BicycleBo> collect = bicycles.stream().map(bicycle1 -> {
            BicycleBo bicycleBo = new BicycleBo(bicycle1.getId(), bicycle1.getBicycleName(), bicycle1.getBicycleBrand(),
                    bicycle1.getColor(), bicycle1.getProductModel(), bicycle1.getPrice(),
                    bicycle1.getProductionDate(), bicycle1.getInventoryCount(), bicycle1.getPic());
            return bicycleBo;
        }).collect(Collectors.toList());
        return collect;
    }




}
